/**
 * 正式环境待添加功能扩展，添加后请删除当前注解
 * getBxllDetailInfo
*/
$NG.AllReady(function (page, { useValuesChange, useClick, useDataIndexChange, useBeforeClick, useUpdateRows, useUpdateRow }) {
    const mstform = $NG.getCmpApi("p_form_wbgyskfpd")
    const dgrid = $NG.getCmpApi("p_form_wbgyskfpd_d")

    if (page.oType == 'add' && page.oType == 'edit') {
        getPtInfoByPhid()
        getCntMInfoByPhid()
        getBztlDetailInfo()
        cntMFilter()
    }
    if (page.oType == 'add') {

    }

    useValuesChange(() => {
        getCntMInfoByPhid()
        getBztlDetailInfo()
    }, "p_form_wbgyskfpd.u_fbht_name")

    useValuesChange(() => {
        getPtInfoByPhid()
        cntMFilter()
    }, "p_form_wbgyskfpd.phid_pc")

    useUpdateRows(({ args }) => {
        sumAmt(args[0])
    }, "p_form_wbgyskfpd_d")


    function sumAmt(rows) {
        rows.forEach(item => {
            item.u_hs_amt = (Number(item.u_hs_prc) || 0) * (Number(item.u_sjsy_qty) || 0)
            item.u_sjsy_qty = (Number(item.u_ly_qty) || 0) - (Number(item.u_sp_qty) || 0)
        })
        dgrid.refreshView()
    }

    //合同根据项目ID过滤
    cntMFilter()
    function cntMFilter() {
        $NG.updateUI((updater, state) => {
            console.log("过滤参数检查", updater);
            updater.form.p_form_wbgyskfpd.u_fbht_name.setProps({
                onBeforeOpen: async () => {
                    var phid_pc = $NG.getCmpApi("p_form_wbgyskfpd").getItem("phid_pc").getValue();
                    if (!phid_pc) {
                        $NG.alert("请先选择项目");
                        return false;
                    } else {
                        return true;
                    }
                },
                clientSqlFilter: {
                    'phid_pc': $NG.getCmpApi("p_form_wbgyskfpd").getItem("phid_pc").getValue(),

                },
            });
        });
    }

    // 获取项目信息
    function getPtInfoByPhid() {
        if (mstform.getItem("phid_pc").getValue()) {
            $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
                const datas = JSON.parse(res.data)[0].extendObjects;
                $NG.updateState((updater) => {
                    updater.data.p_form_wbgyskfpd.setProps({
                        //项目编码
                        u_cnt_no: datas.pc_no,
                    })
                })
            })
        }
    }

    // 获取合同信息
    function getCntMInfoByPhid() {
        if (!mstform.getItem("u_fbht_name").getValue()) return
        $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_fbht_name").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            $NG.updateState((updater) => {
                updater.data.p_form_wbgyskfpd.setProps({
                    u_fbht_no: datas.pcm_no,
                    u_fbgys_name: {
                        label: datas.supply_name,
                        value: datas.supply_no
                    },
                })
            })

        })
    }

    // 获取领料明细
    function getBxllDetailInfo() {
        const cnt = mstform.getItem("u_fbht_name").getValue()
        if (!cnt) return
        $NG.execServer("getBxllDetailInfo", { cnt }, (res) => {
            if (res.count == 0) return
            const _data = JSON.parse(res.data).reduce((value, items) => {
                const item = items.extendObjects
                console.log(`debug 当前缓存`, window._catch_tl_qty);
                let tlqty = window._catch_tl_qty.reduce((value, el) => {
                    if (el.phid_itemdata == item.phid_itemdata) {
                        value = el.qty
                    }
                    return value
                }, 0)

                value.push({
                    u_sbqd_no: item.sbqd_no,
                    u_sbqd_name: item.sbqd_phid,
                    u_sbqd_name_EXName: item.sbqd_name,
                    u_item_no: item.itemno,
                    u_item_name: item.phid_itemdata,
                    u_item_name_EXName: item.itemname,
                    u_ggxh: item.spec,
                    u_jldw: item.phid_unit,
                    u_jldw_EXName: item.msname,
                    u_ly_qty: item.qty - tlqty,
                    u_hs_prc: item.taxprc
                })
                return value
            }, [])
            dgrid.clearRows().then(() => {
                dgrid.addRows(_data)
            })
        })
    }

    // 获取退料信息
    function getBztlDetailInfo() {
        window._catch_tl_qty = []
        const cnt = mstform.getItem("u_fbht_name").getValue()
        if (!cnt) {
            getBxllDetailInfo()
            return
        }
        $NG.execServer("getBztlDetailInfo", { cnt }, (res) => {
            console.log("退料信息响应数据：", res);
            if (res.count == 0) {
                getBxllDetailInfo()
                return
            }
            window._catch_tl_qty = JSON.parse(res.data).reduce((value, items) => {
                value.push(items.extendObjects)
                return value
            }, [])
            getBxllDetailInfo()
        })
    }
})